<?php
/**
 * Created by PhpStorm.
 * User: iceberg
 * Date: 2019-01-29
 * Time: 12:03
 */

namespace app\behavior;

use think\facade\Request;
use traits\controller\Jump;

class CheckAuth
{
    use Jump; //引入trait
    public function run()
    {
        //获取 model/controller/action
        $path = strtolower(Request::module()) . '/' . strtolower(Request::controller()) . '/' . strtolower(Request::action());
        $pathController = strtolower(Request::module()) . '/' . strtolower(Request::controller()) . '/*';
        //排除的地址放行
        if(in_array($path,config('rbac.OTHER')) == false){
            //判断用户是否登录
            $role = session('role');
            if($role){
                //当用户不是超级管理员的时候权限判断
                if($role > 1){
                    $rbac_role_auths = config('rbac.RBAC_ROLE_AUTHS')[$role];

                    if(!in_array($path, $rbac_role_auths) && !in_array($pathController, $rbac_role_auths)){
                        //用户没有权限
                        $this->error('您没有权限！','/system/sign/login');
                    }
                }
            }else{
                //没有登录则跳转到登录页面

                return $this->redirect('/system/sign/login');
            }
        }
    }
}